import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import javax.print.MultiDocPrintService;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

public class jdbclearn {
         private Connection connection;

    public static void main(String[] args) throws Exception {
        jdbclearn jdbclearn = new jdbclearn();
       // jdbclearn.insertDate();
       // jdbclearn.delateDate();
        //jdbclearn.updateDate();
    }
    public jdbclearn() throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/druidok.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection = dataSource.getConnection();
    }

    @Test
    public void insertDate(String sql)  throws ClassNotFoundException, SQLException {
        //3.实现Crud
        sql="insert into stu_mark values (null ,?,?,?)";
        stu_mark stu_mark = new stu_mark();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的姓名");
        String name = scanner.next();
        System.out.println("请输入你的专业");
        String major = scanner.next();
        System.out.println("请输入你的成绩");
        String mark = scanner.next();
        stu_mark.setName(name);
        stu_mark.setMajor(major);
        stu_mark.setMark(mark);
//        String sql = "";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,stu_mark.getName());
        statement.setString(2,stu_mark.getMajor());
        statement.setString(3,stu_mark.getMark());
        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("插入 " + i);
        //6.关闭资源
        statement.close();
    }
    public void selectDate()  throws ClassNotFoundException, SQLException {
        ArrayList<stu_mark> stu_marks = new ArrayList<>();
        //        //3.实现Crud
       String sql = "select *from stu_mark";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        //5.执行sql,获取结果集
        ResultSet resultSet = statement.executeQuery();
        //6.通过next指向结果集第一行
        while (resultSet.next()) {
            stu_mark stu_mark = new stu_mark();

            //7.解析结果中数据字段
            stu_mark.setName(resultSet.getString("name"));
            stu_mark.setMajor(resultSet.getString("major"));
            stu_mark.setMark(resultSet.getString("mark"));
            stu_marks.add(stu_mark);
        }
        for (int i = 0; i < stu_marks.size(); i++) {
            System.out.println("stu_marks.get(i).getName() = " + stu_marks.get(i).getName());
            System.out.println("stu_marks.get(i).getMajor() = " + stu_marks.get(i).getMajor());
            System.out.println("stu_marks.get(i).getMark() = " + stu_marks.get(i).getMark());
        }
        //8.关闭资源
        statement.close();
    }
    public void delateDate(String sql)  throws ClassNotFoundException, SQLException {
//        //3.实现Crud
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,8);
        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("删除 " + i);
        //6.关闭资源
        statement.close();

    }
    public void updateDate(String sql)  throws ClassNotFoundException, SQLException {

//        //3.实现Crud
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,"15415347615275");
        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("修改 " + i);
        //6.关闭资源
        statement.close();

    }
}
